﻿using CorpStore.Models;
using Newtonsoft.Json;
using StoreHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace CorpStore.ViewModels
{
    /// <summary>
    /// This class contains various I/O methods used by most all view models to 
    /// access the masterCatalog.json file.
    /// </summary>
    class PackageIO
    {
        public static async Task<PackageInfoGroupList> LoadApplicationGroupsAsync()
        {
            PackageInfoGroupList groupList = null;
            try
            {
                if (await GenIO.FileExistsAsync(ApplicationData.Current.LocalFolder, "MasterCatalog.json"))
                {
                    StorageFile catalogFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/MasterCatalog.json"));
                    string rawData = await FileIO.ReadTextAsync(catalogFile);

                    groupList = JsonConvert.DeserializeObject<PackageInfoGroupList>(rawData);
                    fixImageFilenames(groupList);
                }
                else
                    App.LogErr("Cannot load applications groups since master catalog has not been created");
            }
            catch (Exception ex)
            {
                App.LogErr("Unhandled exception while loading application groups: {0}", ex.Message);
            }

            return groupList;
        }


        /// <summary>
        /// This method overrides any previously loaded filenames for LogoFn, 
        /// SplashScreenFn, and filenames for screenshots such that all filenames
        /// are found in //local/AppFiles directory.  This is necessary to 'fixup'
        /// the filenames originally stored in masterCatalog.json.
        /// </summary>
        /// <param name="groupList"></param>
        private static void fixImageFilenames(PackageInfoGroupList groupList)
        {
            // fix normal app groups images filenames
            foreach (PackageInfoGroup group in groupList.Groups)
            {
                foreach (PackageInfo app in group.Applications)
                {
                    app.LogoFn = string.Format(@"ms-appdata:///Local/AppFiles/{0}_logo.png", app.Name);
                    app.SplashScreenFn = string.Format(@"ms-appdata:///Local/AppFiles/{0}_splashscreen.png", app.Name);
                    for (int i = 0; i < app.Screenshots.Count; i++)
                    {
                        ScreenshotInfo info = app.Screenshots[i];
                        info.Filename = string.Format(@"ms-appdata:///Local/AppFiles/{0}_screenshot{1}.png", app.Name, i);
                    }
                }
            }

            // fix featured app images filenames
            foreach (PackageInfo app in groupList.FeaturedApps)
            {
                app.LogoFn = string.Format(@"ms-appdata:///Local/AppFiles/{0}_logo.png", app.Name);
                app.SplashScreenFn = string.Format(@"ms-appdata:///Local/AppFiles/{0}_splashscreen.png", app.Name);
                for (int i = 0; i < app.Screenshots.Count; i++)
                {
                    ScreenshotInfo info = app.Screenshots[i];
                    info.Filename = string.Format(@"ms-appdata:///Local/AppFiles/{0}_screenshot{1}.png", app.Name, i);
                }
            }
        }
    }
}
